package com.briup.smartlabs.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.briup.smartlabs.bean.ex.DeviceType;
import com.briup.smartlabs.common.exceptions.DataValidateException;
import com.briup.smartlabs.common.utils.Response;
import com.briup.smartlabs.common.utils.ResponseCodeEnum;
import com.briup.smartlabs.service.DeviceTypeService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/*
 * 所有和设备类型相关的服务
 */
@Api(tags = "设备类型服务")
@RestController
@RequestMapping(value = "/deviceType")
public class DeviceTypeController {
	@Autowired
	private DeviceTypeService impl;

	@GetMapping(value = "/page/by/condition")
	public Response<PageInfo<DeviceType>> findByCondition(int pageSize,int pageNum, String key){

		// 调用service层代码
		PageInfo<DeviceType> deviceTypeByPage = impl.findByCondition(pageSize, pageNum, key);

		return Response.ok(deviceTypeByPage);
	}

	//可以先不处理
	/*
		逻辑上比较复杂
	    要求批量删除的时候，返回一个map，标记出来哪些删除成功哪些删除失败
	    {
	   		"success": [1,3,5],
	   		"failure": [2,4,6]
	     }
	 */
	@DeleteMapping("/batch")
	public Response<Map<String,List<String>>> deleteByBatch(String[] id) {
		return Response.ok(null);
	}

	//按照id删除设备类型
	@DeleteMapping("/{id}")
	public Response<String> deleteById(@PathVariable String id) {
		// 调用service层的代码
		// 希望能够通过异常捕获返回不同的结果
		try {
			impl.removeById(id);
		}catch (RuntimeException e){
			return Response.error(ResponseCodeEnum.DATA_NOT_FOUND);
		}
		return Response.ok("删除设备成功！");
	}

	//保存或更新设备类型服务
	@PostMapping("/saveOrUpdate")
	public Response<String> addType(@RequestBody DeviceType type) {
		// 输出一下type的名称
		System.out.println(type.getName());
		// 调用service层的方法
		impl.saveOrUpdate(type);
		return Response.ok("操作成功！");
	}

}








